package com.sh1604.duitang.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.sh1604.duitang.R;
import com.sh1604.duitang.adapter.FindPagerAdapter;
import com.sh1604.duitang.constant.UrlString;
import com.sh1604.duitang.mybean.ImageBean;
import com.sh1604.duitang.utils.OKHttpUtils;
import com.sh1604.duitang.view.FindImageView;
import com.sh1604.duitang.view.FindSecondView;
import com.sh1604.duitang.view.FindTopView;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindFragment extends Fragment {

    private Activity mActivity;
    private FindTopView topView;
    private FindSecondView secondView;
    private FindImageView imageView;
    private ArrayList<ImageBean.DataBean.GroupItemsBean> mDatas;
    private FindPagerAdapter adapter;
    private ListView gv;

    public FindFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        mDatas = new ArrayList<>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        topView = new FindTopView(mActivity);
        secondView = new FindSecondView(mActivity);
        imageView = new FindImageView(mActivity);
        gv = (ListView) view.findViewById(R.id.gv);
        adapter = new FindPagerAdapter(mActivity,mDatas);
        //gv.addHeaderView(topView);
        //gv.addHeaderView(secondView);
        //gv.addHeaderView(imageView);
        gv.setAdapter(adapter);
        OKHttpUtils.newInstance(getContext()).getAsyncMainData(UrlString.URL_FIND, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                final ImageBean bean = new Gson().fromJson(result,ImageBean.class);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < bean.getData().size(); i++) {
                            mDatas.addAll(bean.getData().get(i).getGroup_items());
                        }
                        Log.i("mDatas","--->--->!!!--->--->"+mDatas.size());
                    }
                });
            }
        });
    }
    Handler mHandler = new Handler();
}
